home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / gfa / gfaexprt.lzh / GFAXPERT.LIB / SORTSTR.LST < prev    next >
Encoding:
File List  |  1986-10-19  |  2.7 KB  |  100 lines

  1. ' *******************
  2. ' *** SORTSTR.LST ***
  3. ' *******************
  4. '
  5. DEFWRD "a-z"
  6. '
  7. > PROCEDURE bin.search.string(element$,VAR proc$(),index)
  8.   ' *** find element$ in sorted string array (binary search)
  9.   ' *** global :   FOUND!
  10.   LOCAL first,last,middle
  11.   first=1
  12.   last=DIM?(proc$())-1
  13.   WHILE first<last
  14.     middle=DIV(ADD(first,last),2)
  15.     IF element$>proc$(middle)
  16.       first=ADD(middle,1)
  17.     ELSE
  18.       last=middle
  19.     ENDIF
  20.   WEND
  21.   found!=(proc$(first)=element$)
  22.   IF found!
  23.     index=first
  24.   ELSE
  25.     index=0
  26.   ENDIF
  27. RETURN
  28. ' **********
  29. '
  30. > PROCEDURE ascii.qsort(VAR txt$())
  31.   ' *** 'true' alphabetical sorting of string-array
  32.   IF DIM?(ascii|())=0
  33.     @initio.ascii.array
  34.   ENDIF
  35.   QSORT txt$() WITH ascii|()
  36. RETURN
  37. ' ***
  38. > PROCEDURE initio.ascii.array
  39.   ' *** ASCII byte-array to be used with QSORT (or SSORT)
  40.   ' *** global :  ASCII|()
  41.   LOCAL i,code1,code2
  42.   DIM ascii|(255)
  43.   ARRAYFILL ascii(),32           ! fill with space-character
  44.   FOR i=48 TO 57
  45.     ascii|(i)=i                  ! 0 - 9
  46.   NEXT i
  47.   FOR i=65 TO 90
  48.     ascii|(i)=i                  ! A - Z
  49.   NEXT i
  50.   FOR i=97 TO 122
  51.     ascii|(i)=SUB(i,32)          ! a - z, converted to A - Z
  52.   NEXT i
  53.   RESTORE ascii.data
  54.   REPEAT
  55.     READ code1,code2
  56.     ascii|(code1)=code2
  57.   UNTIL code1=0
  58.   '
  59.   ascii.data:
  60.   ' *** format : ASCII-code,replacement
  61.   DATA 128,67,129,85,130,69,131,65,132,65,133,65,134,65,135,67,136,69,137,69
  62.   DATA 138,69,139,73,140,73,141,73,142,65,143,65,144,69,145,65,146,65,147,79
  63.   DATA 148,79,149,79,150,85,151,85,152,121,153,79,154,85,155,67,158,83,160,65
  64.   DATA 161,73,162,79,163,85,164,78,165,78,166,65,167,79,176,65,177,79,178,79
  65.   DATA 179,79,180,79,181,79,182,65,183,65,184,79,192,121,193,121,225,83,0,0
  66. RETURN
  67. ' **********
  68. '
  69. > PROCEDURE string.index.qsort(switch!,VAR txt$(),index%())
  70.   ' *** fills index-array with index-numbers of sorted string-array
  71.   ' *** string-array txt$() is not changed !
  72.   ' *** the index-array has to exist already (DIM before calling this Procedure)
  73.   ' *** if switch!=TRUE, array ascii|() is used for 'true' alphabetical sorting
  74.   ' *** all elements (0 - last) are sorted, element txt$(0) is NOT ignored !!
  75.   ' *** index-array should be used like this :
  76.   '                   FOR i=0 TO last
  77.   '                     PRINT txt$(index%(i))
  78.   '                   NEXT i
  79.   LOCAL last,i
  80.   last=DIM?(txt$())-1           ! index of last element
  81.   DIM temp$(last)
  82.   FOR i=0 TO last
  83.     temp$(i)=txt$(i)
  84.   NEXT i
  85.   FOR i=0 TO last
  86.     index%(i)=i
  87.   NEXT i
  88.   IF switch!
  89.     IF DIM?(ascii|())=0
  90.       @initio.ascii.array
  91.     ENDIF
  92.     QSORT temp$() WITH ascii|(),last+1,index%()
  93.   ELSE
  94.     QSORT temp$(),last+1,index%()
  95.   ENDIF
  96.   ERASE temp$()
  97. RETURN
  98. ' **********
  99. '
  100.